--[[
* MapNotes 2.0
* File: MapNotes\MapNotes_CategoryDropDown.lua
* $Revision: 132 $
* $Date: 2007-01-06 13:08:56 +0000 (Sat, 06 Jan 2007) $
* 
* Copyright (c) 2006-2007, Mery (Moritz Metelmann)
* All rights reserved.
* 
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* 
*    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*    * Neither the name of the <ORGANIZATION> nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
]]--

if (not MapNotes) then MapNotes = {}; end
if (not MapNotes.Category) then MapNotes.Category = {}; end
if (not MapNotes.Category.DropDown) then MapNotes.Category.DropDown = {}; end

local L = MapNotes.Localization;

MapNotes.Category.generalCategory = {name = L["General Notes"], defaultIcon = "DEFAULT_CATEGORY"};
MapNotes.Category.receivedCategory = {name = L["Received Notes"], defaultIcon = "RECEIVED_CATEGORY"};

MapNotes.Category.DropDown.OnLoad = function()
	MapNotes.Category.DropDown.showDefault = 1;
end

MapNotes.Category.DropDown.ScrollBarUpdate = function()
	-- highlight!!
  local line; -- 1 through 5 of our window to scroll
  local lineplusoffset; -- an index into our data calculated from the scroll offset
  FauxScrollFrame_Update(MapNotesCategoryDropDownScrollBar,getn(MapNotesSaved_Category.categories)+MapNotes.Category.DropDown.showDefault*2,7,16); --85 is correct
  for line=1,7 do
		lineplusoffset = FauxScrollFrame_GetOffset(MapNotesCategoryDropDownScrollBar) + line - MapNotes.Category.DropDown.showDefault*2;
		if (lineplusoffset <= getn(MapNotesSaved_Category.categories)) then
			local category;
			if (lineplusoffset == -1) then category = MapNotes.Category.generalCategory; 
				elseif(lineplusoffset == 0) then category = MapNotes.Category.receivedCategory;
				else category = MapNotesSaved_Category.categories[lineplusoffset]; end;
			getglobal("MapNotesCategoryDropDownButton"..line.."Text"):SetText(category.name);
			getglobal("MapNotesCategoryDropDownButton"..line):Show();
			local texture = getglobal("MapNotesCategoryDropDownButton"..line.."Icon");
			local icon = MapNotes.Icon.GetIconTexture(category.defaultIcon);
			texture:SetTexture(icon.file);
			if (icon.tcoordMinX) then texture:SetTexCoord(icon.tcoordMinX, icon.tcoordMaxX, icon.tcoordMinY, icon.tcoordMaxY); else texture:SetTexCoord(0, 1, 0, 1); end;
			if (icon.colorR) then texture:SetVertexColor(icon.colorR, icon.colorG, icon.colorB, icon.colorA); else texture:SetVertexColor(1, 1, 1, 1); end;
		else
			getglobal("MapNotesCategoryDropDownButton"..line):Hide();
		end;
  end
end

MapNotes.Category.DropDown.ButtonOnEnter = function()
	getglobal(this:GetName().."Highlight"):Show();
end

MapNotes.Category.DropDown.ButtonOnLeave = function()
	getglobal(this:GetName().."Highlight"):Hide();
end

MapNotes.Category.DropDown.Show = function(highlight, showDefault, callback, parent, frameLevel, point, relativeTo, relativePoint, x, y)
	MapNotes.Category.DropDown.showDefault = showDefault;
	MapNotes.Category.DropDown.callback = callback;
	MapNotesCategoryDropDownMenu:SetParent(parent);
	MapNotesCategoryDropDownMenu:SetPoint(point, relativeTo, relativePoint, x, y);
	
	MapNotesCategoryDropDownMenu:SetFrameLevel(frameLevel);
	MapNotesCategoryDropDownScrollBarScrollBarScrollUpButton:SetFrameLevel(frameLevel+1);
	MapNotesCategoryDropDownScrollBarScrollBarScrollDownButton:SetFrameLevel(frameLevel+1);
	MapNotesCategoryDropDownScrollBarScrollBar:SetFrameLevel(frameLevel+1);
	MapNotesCategoryDropDownScrollBar:SetFrameLevel(frameLevel+1);
	for i=1, 7, 1 do
		getglobal("MapNotesCategoryDropDownButton"..i):SetFrameLevel(frameLevel+1);
	end;
	
	MapNotesCategoryDropDownMenu:Show();
end

MapNotes.Category.DropDown.Hide = function()
	MapNotesCategoryDropDownMenu:Hide();
end

MapNotes.Category.DropDown.IsVisible = function()
	return MapNotesCategoryDropDownMenu:IsVisible();
end

MapNotes.Category.DropDown.OnClick = function()
	lineplusoffset = FauxScrollFrame_GetOffset(MapNotesCategoryDropDownScrollBar) + this:GetID() - MapNotes.Category.DropDown.showDefault*2;
	local category;
	if (lineplusoffset == -1) then category = MapNotes.Category.generalCategory; 
	elseif(lineplusoffset == 0) then category = MapNotes.Category.receivedCategory;
	else category = MapNotesSaved_Category.categories[lineplusoffset]; end;
	MapNotes.Category.DropDown.callback(category.name);
end;

MapNotes.Category.GetDefaultIconID = function(categoryName)
	if (categoryName == MapNotes.Category.generalCategory.name) then
		return MapNotes.Category.generalCategory.defaultIcon;
	elseif (categoryName == MapNotes.Category.receivedCategory.name) then
		return MapNotes.Category.receivedCategory.defaultIcon;
	else
		for _, value in ipairs(MapNotesSaved_Category.categories) do
			if (categoryName == value.name) then
				return value.defaultIcon;
			end;
		end;
	end;
	return nil;
end;